﻿'Pour tester le check in
Public Class Frm_Ref1
    Public Sub remplirLstViewRef()
        Partager.moi.remplirRef()
        List_reference.Items.Clear()
        For Each refc In Partager.moi.List_Reference
            Partager.line = New ListViewItem(New String() {refc.Ref, refc.dateInscriptionBur})
            List_reference.Items.Add(Partager.line)
        Next
        Partager.rempliCombClient(Comb_Ref, "select distinct Ref from Reference", "Ref")
        Partager.setfMarji3(Comb_Ref)
        For Each it As ListViewItem In List_reference.Items
            it.Selected = False
            it.Focused = False
        Next
    End Sub
    Private Sub Frm_Ref1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Frm_Liaison_Ref.Enabled = True
        If List_reference.SelectedItems.Count > 0 Then
            Frm_Liaison_Ref.txt_Ref.Text = List_reference.SelectedItems(0).SubItems(0).Text
        End If
    End Sub

    Private Sub Frm_Ref1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            remplirLstViewRef()
            Partager.setfMarji3(Comb_Ref)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.setfMarji3(Comb_Ref)
        For Each it As ListViewItem In List_reference.Items
            it.Selected = False
            it.Focused = False
        Next
    End Sub

    Private Sub Btn_ajou_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajou.Click
        Try
            If Comb_Ref.Text <> "" Then
                'Dim ref As Integer
                Dim compteur As Integer = 0
                Partager.refObj = New Reference(CInt(Comb_Ref.Text), Date_insc_bur.Value)
                Partager.moi.ajouter_Ref(Partager.refObj)
                'ref = CInt(Comb_Ref.Text)

                remplirLstViewRef()
                'For Each it As Integer In Comb_Ref.Items
                '    If it = ref Then
                '        Comb_Ref.SelectedIndex = compteur
                '    End If
                '    compteur += 1
                'Next
                MsgBox("تمت إضافة المرجع بنجاح", MsgBoxStyle.Information, "نجاح")
            Else
                MsgBox("المرجو اختيار المرجع أولا", MsgBoxStyle.Critical, "المرجع")
            End If
        Catch sqlMsg As SqlClient.SqlException
            If sqlMsg.Message.Contains("PK_Reference") Then
                MsgBox("تمت إضافة هذا المرجع من قبل", MsgBoxStyle.Critical, "خطأ")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Comb_Ref_KeyPress1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Comb_Ref.KeyPress
        Dim allowedChars As String = "1234567890"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_Ref_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_Ref.SelectedIndexChanged
        If Not Partager.moi.List_Reference Is Nothing Then
            Dim comp As Integer = 0
            For Each refc In Partager.moi.List_Reference
                If refc.Ref = CInt(Comb_Ref.Text) Then
                    Date_insc_bur.Value = refc.dateInscriptionBur
                    List_reference.Items(comp).Focused = True
                    List_reference.Items(comp).Selected = True
                    Exit For
                End If
                comp += 1
            Next
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        Try
            If List_reference.SelectedIndices.Count > 0 Then
                If MsgBox("هل تريد فعلا حذف هذا المرجع ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف") = MsgBoxResult.Yes Then
                    Dim i As Integer = List_reference.SelectedIndices(0)
                    Partager.moi.supprimerRef(Partager.moi.List_Reference(i))
                    remplirLstViewRef()
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub List_reference_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles List_reference.DoubleClick
        Me.Close()
    End Sub

    Private Sub List_reference_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_reference.SelectedIndexChanged
        If List_reference.SelectedIndices.Count > 0 Then
            Dim comp As Integer = 0
            For Each refc In Partager.moi.List_Reference
                If refc.Ref = CInt(List_reference.SelectedItems(0).SubItems(0).Text) Then
                    Partager.refObj = refc
                    Date_insc_bur.Value = refc.dateInscriptionBur
                    Comb_Ref.SelectedIndex = comp
                    Comb_Ref.Text = List_reference.SelectedItems(0).SubItems(0).Text
                    Exit For
                End If
                comp += 1
            Next
        End If
    End Sub
End Class